<?php

namespace app\admin\controller\ykjp\sell;

use app\common\controller\Backend;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Returnaudit extends Backend {

    /**
     * Returnaudit模型对象
     * @var \app\admin\model\ykjp\sell\Returnaudit
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\sell\Returnaudit;
        $this->view->assign("statusListList", $this->model->getStatusListList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 详情
     */
    public function detail() {
        $ids = $this->request->param("ids");
        if ($ids == "") {
            $this->error("fail", "ykjp/sell/Sellreturn/index");
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = Db::name('ykjp_sell_return')
                ->where($where)
                ->where('firmid', $this->auth->firmid)
                ->where("id", $ids)
                ->select();

        $dModel = new \app\admin\model\ykjp\sell\returnPro;
        $strProduct = $dModel->with(['products'])->where("sell_return_id", $ids)->where('products.firmid', $this->auth->firmid)->select();
        // halt($strProduct);

        $this->assign("detail", $list);
        $this->assign("product", $strProduct);
        return $this->view->fetch();
    }

    /**
     * 审核
     */
    public function approval($ids = null) {


        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        //判断通过还是驳回 （pass 通过  reject 驳回)
        if ($this->request->param("type") == "yes") {  //通过
            $data = array("status_list" => 3, "reviewer_id" => $this->auth->id, "updatetime" => time());
            $update = $this->model
                    ->where("status_list", 1)
                    ->where('firmid', $this->auth->firmid)
                    ->where("id", "in", $ids)
                    ->update($data);
            if ($update) {
                $this->success(null, null, array("result" => "success"));
            }
            $this->error("operation fail");
        } elseif ($this->request->param("type") == "no") {  //驳回
            $cause = $this->request->param('cause');  //审核失败原因
            $data = array("status_list" => 2, "reviewer_id" => $this->auth->id, "updatetime" => time(), 'cause' => $cause);
            $update = $this->model
                    ->where("status_list", 1)
                    ->where('firmid', $this->auth->firmid)
                    ->where("id", "in", $ids)
                    ->update($data);

            if ($update) {
                $this->success(null, null, array("result" => "success"));
            }
            $this->error("operation fail");
        }

        $this->error("Illegal operation");
    }

}
